<?php
namespace App\Http\Controllers\Member;

use App\Models\BbsQuestion;
use App\Models\BbsAnswer;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
/**
 *
 * @author Joker
 *        
 */
class QuestionController extends MemberController
{
    
    /**
     * 我的帖子
     */
    public function index()
    {
        $sort = Input::get('sort',0);
        $query = BbsQuestion::where(array('publish_uid'=>$this->uid));
        switch ($sort){
            case 1:  //浏览量
                $query->orderBy('view_count','desc');
                break;
            case 2:  //回复数
                $query->orderBy('comment_count','desc');
                break;
            default:
                $query->orderBy('id','desc');
        }
        $question = $query->orderBy('updated_at','desc')->orderBy('created_at','desc')->paginate(10);
        return view('member.bbs.question')->with('question',$question)->with('sort',$sort);
    }


    /**
     * 删除
     */
    public function destroy($id)
    {
        $question = BbsQuestion::where(array('id'=>$id,'publish_uid'=>$this->uid))->first();
        if(empty($question))
        {
            return  $this->formatJson('falied','抱歉，资源不存在!');
        }
        else
        {
            DB::transaction(function() use ($question){
                BbsAnswer::where(array('question_id'=>$question->id,'user_id'=>$this->uid))->delete();
                $question->delete();
            });
            return $this->formatJson('success','删除帖子成功！');
        }
    }
}